package com.daowen.action;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.SQLQuery;
import org.hibernate.Query;
import com.daowen.dal.*;
import com.daowen.bll.*;
import com.daowen.entity.*;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.text.MessageFormat;
import com.daowen.util.PagerMetal;

/**************************
 *
 * @author daowen
 *
 *         相片控制
 *
 */
public class AlbumitemAction extends PageActionBase {
    @Override
    public void onLoad() {
        String actiontype = request.getParameter("actiontype");
        System.out.println("actiontype=" + actiontype);
        if (actiontype == null)
            return;
    }

    /********************************************************
     ****************** 信息注销监听支持*****************************
     *********************************************************/
    public void delete() {
        String id = request.getParameter("id");
        DALBase.delete("albumitem", " where id=" + id);
        binding();
    }

    /*************************************************************
     **************** 保存动作监听支持******************************
     **************************************************************/
    public void save() {
        String forwardurl = request.getParameter("forwardurl");
        // 验证错误url
        String errorurl = request.getParameter("errorurl");
        String tupian = request.getParameter("tupian");
        String albumid = request.getParameter("albumid");
        SimpleDateFormat sdfalbumitem = new SimpleDateFormat("yyyy-MM-dd");
        Albumitem albumitem = new Albumitem();
        albumitem.setTupian(tupian == null ? "" : tupian);
        albumitem.setAlbumid(albumid == null ? "" : albumid);
        DALBase.save(albumitem);
        // 保存附件
        // attachments(request,response,new
        // Integer(albumitem.getId()).toString());
        if (forwardurl == null) {
            forwardurl = "/admin/albumitemmanager.do?actiontype=get";
        }
        try {
            response.sendRedirect(SystemParam.getSiteRoot() + forwardurl);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /******************************************************
     *********************** 内部附件支持*********************
     *******************************************************/
    public void attachments(String belongid) {
        DALBase.delete("attachement", MessageFormat.format(
                " where belongid=''{0}'' and belongtable=''albumitem'' ",
                belongid));
        String[] photos = request.getParameterValues("fileuploaded");
        if (photos == null)
            return;
        for (int i = 0; i < photos.length; i++) {
            Attachement a = new Attachement();
            a.setType("images");
            a.setPubtime(new Date());
            a.setBelongfileldname("id");
            a.setFilename(photos[i]);
            a.setBelongid(belongid);
            a.setBelongtable("albumitem");
            a.setUrl(SystemParam.getSiteRoot() + "/upload/temp/"
                    + a.getFilename());
            a.setTitle(a.getFilename());
            DALBase.save(a);
        }
    }

    /******************************************************
     *********************** 更新内部支持*********************
     *******************************************************/
    public void update() {
        String forwardurl = request.getParameter("forwardurl");
        String id = request.getParameter("id");
        if (id == null)
            return;
        Albumitem albumitem = (Albumitem) DALBase.load(Albumitem.class,
                new Integer(id));
        if (albumitem == null)
            return;
        String tupian = request.getParameter("tupian");
        String albumid = request.getParameter("albumid");
        SimpleDateFormat sdfalbumitem = new SimpleDateFormat("yyyy-MM-dd");
        albumitem.setTupian(tupian);
        albumitem.setAlbumid(albumid);
        DALBase.update(albumitem);
        // attachments(new Integer(albumitem.getId()).toString());
        if (forwardurl == null) {
            forwardurl = "/admin/albumitemmanager.do?actiontype=get";
        }
        try {
            response.sendRedirect(SystemParam.getSiteRoot() + forwardurl);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /******************************************************
     *********************** 加载内部支持*********************
     *******************************************************/
    public void load() {
        //
        String id = request.getParameter("id");
        String actiontype = "save";
        dispatchParams(request, response);
        if (id != null) {
            Albumitem albumitem = (Albumitem) DALBase.load("albumitem",
                    "where id=" + id);
            if (albumitem != null) {
                request.setAttribute("albumitem", albumitem);
            }
            actiontype = "update";
            request.setAttribute("id", id);
        }
        request.setAttribute("actiontype", actiontype);
        String forwardurl = request.getParameter("forwardurl");
        System.out.println("forwardurl=" + forwardurl);
        if (forwardurl == null) {
            forwardurl = "/admin/albumitemadd.jsp";
        }
        try {
            request.getRequestDispatcher(forwardurl).forward(request, response);
        } catch (ServletException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /******************************************************
     *********************** 数据绑定内部支持*********************
     *******************************************************/
    public void binding() {
        String filter = "where 1=1 ";
        String tupian = request.getParameter("tupian");
        if (tupian != null)
            filter += "  and tupian like '%" + tupian + "%'  ";
        //
        String albumid=request.getParameter("albumid");
        if(albumid!=null)
            filter+=" and albumid='"+albumid+"'";

        int pageindex = 1;
        int pagesize = 10;
        // 获取当前分页
        String currentpageindex = request.getParameter("currentpageindex");
        // 当前页面尺寸
        String currentpagesize = request.getParameter("pagesize");
        // 设置当前页
        if (currentpageindex != null)
            pageindex = new Integer(currentpageindex);
        // 设置当前页尺寸
        if (currentpagesize != null)
            pagesize = new Integer(currentpagesize);
        List<Albumitem> listalbumitem = DALBase.getPageEnity("albumitem",
                filter, pageindex, pagesize);
        int recordscount = DALBase.getRecordCount("albumitem",
                filter == null ? "" : filter);
        request.setAttribute("listalbumitem", listalbumitem);
        PagerMetal pm = new PagerMetal(recordscount);
        // 设置尺寸
        pm.setPagesize(pagesize);
        // 设置当前显示页
        pm.setCurpageindex(pageindex);
        // 设置分页信息
        request.setAttribute("pagermetal", pm);
        // 分发请求参数
        dispatchParams(request, response);
        String forwardurl = request.getParameter("forwardurl");
        System.out.println("forwardurl=" + forwardurl);
        if (forwardurl == null) {
            forwardurl = "/admin/albumitemmanager.jsp";
        }
        try {
            request.getRequestDispatcher(forwardurl).forward(request, response);
        } catch (ServletException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
